package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("order")
@Api(tags = "移动端-订单管理")
public class OrderController {

    @Reference
    private OrderService orderService;

    @GetMapping("sendCode/{telephone}")
    @ApiOperation(value = "短信发送", notes = "用户预约验证码发送说明")
    public Result sendCode(@PathVariable("telephone") String telephone) {
        //生成4位数的验证码
        String code = ValidateCodeUtils.generateValidateCode(4);
        //发送短信
        //SmsUtils.validUserTelephone(telephone, code);
        System.out.println("验证码为：" + code);
        //将验证码保存到Redis,并设置超时时间
        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER + telephone, code,90, TimeUnit.SECONDS);

        return new Result(code);
    }


    @PostMapping("add")
    @ApiOperation(value = "预约提交表单效验", notes = "用户提交的数据")
    public Result add(@RequestBody Map map){
        return new Result(orderService.add(map));
    }


    //移动端根据订单id查询订单信息
    @GetMapping("findOrderInfoByOrderId/{id}")
    public Result findOrderInfoByOrderId(@PathVariable("id") int id){
        return new Result(orderService.findOrderInfoByOrderId(id));
    }
}
